//package com.kgw.gatewaybluetooth.service;
//
//import android.app.Service;
//import android.bluetooth.BluetoothAdapter;
//import android.bluetooth.BluetoothDevice;
//import android.bluetooth.BluetoothGatt;
//import android.bluetooth.BluetoothGattCallback;
//import android.bluetooth.BluetoothGattCharacteristic;
//import android.bluetooth.BluetoothGattDescriptor;
//import android.bluetooth.BluetoothGattService;
//import android.bluetooth.BluetoothManager;
//import android.content.Context;
//import android.content.Intent;
//import android.os.Binder;
//import android.os.Handler;
//import android.os.IBinder;
//import android.support.annotation.Nullable;
//import android.util.Log;
//
//import com.kgw.gatewaybluetooth.GbApplication;
//
//import java.lang.reflect.Method;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Timer;
//import java.util.TimerTask;
//import java.util.UUID;
//
///**
// * Created by jaydenma on 16/7/29.
// */
//public class BluetoothService extends Service {
//
//    private static final String CHARACTERISTIC_UUID_RETURN = "00002902-0000-1000-8000-00805f9b34fb";
//    private static final String CH_UUID_Notify = "0000fff2-0000-1000-8000-00805f9b34fb";
//    private static final String SERVICE_UUID = "00001910-0000-1000-8000-00805f9b34fb";
//    private String Tag = "BluetoothService";
//    private String bleAddress;
//    Handler handler = new Handler() {
//    };
//    private BLEBinder ibinder = new BLEBinder();
//    private boolean isScan = false;
//    private List<BluetoothDevice> listDevice = new ArrayList();
//    private BluetoothAdapter mBluetoothAdapter = null;
//    private BluetoothDevice mBluetoothDevice = null;
//    private BluetoothGatt mBluetoothGatt = null;
//    private BluetoothGattCharacteristic mBluetoothGattCharacteristic_Notify = null;
//    private BluetoothManager mBluetoothManager = null;
//
//    private TimerTask taskDis;
//    private long time = 0L;
//    private Timer timer;
//    private Timer timerDis;
//    private TimerTask timerTask;
//
//    public BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
//        public void onLeScan(BluetoothDevice paramAnonymousBluetoothDevice, int paramAnonymousInt, byte[] paramAnonymousArrayOfByte) {
////            if ((paramAnonymousBluetoothDevice.getName() != null) && (!listDevice.contains(paramAnonymousBluetoothDevice))) {
////                listDevice.add(paramAnonymousBluetoothDevice);
////                Log.e(Tag, "-----Name=" + paramAnonymousBluetoothDevice.getName() + "   Address=" + paramAnonymousBluetoothDevice.getAddress());
//            if (paramAnonymousBluetoothDevice.getName().equals("FSRKB-EWQ01")) {
//                stopTimer();
//                mBluetoothAdapter.stopLeScan(leScanCallback);
//            }
////            }
//
//            try {
//                Thread.sleep(300L);
//                timetaskConnect();
//                connect(paramAnonymousBluetoothDevice.getAddress());
//                return;
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//    };
//
//    BluetoothGattCallback mGattcallback = new BluetoothGattCallback() {
//        public void onCharacteristicChanged(BluetoothGatt paramAnonymousBluetoothGatt, BluetoothGattCharacteristic paramAnonymousBluetoothGattCharacteristic) {
//            Log.e("data",paramAnonymousBluetoothGattCharacteristic.getValue().toString());
////            for (int i = 0; i < paramAnonymousBluetoothGattCharacteristic.getValue().length; i++)
////                Log.e(BluetoothService.this.Tag, "byte" + i + "===" + paramAnonymousBluetoothGattCharacteristic.getValue()[i]);
////            String str1 = BitMapTools.bytesToHexString(paramAnonymousBluetoothGattCharacteristic.getValue());
////            Log.e("BLE code", "###返回##" + str1);
////            String str2;
////            String str3;
////            String str4;
////            if ((str1.substring(0, 10).equals("0220dd08ff")) && (System.currentTimeMillis() - BluetoothService.this.time > 3000L))
////            {
////                BluetoothService.access$502(BluetoothService.this, System.currentTimeMillis());
////                str2 = str1.substring(10, 12);
////                str3 = str1.substring(12, 14);
////                if (!str2.equals("ee"))
////                    break label350;
////                str4 = "Err";
////                if (!str3.equals("01"))
////                    break label215;
////                str4 = BluetoothService.this.getResources().getString(2131165427);
////            }
////            while (true)
////            {
////                sendBroadCast("ble.MeasureErr", "ErrMSG", str4);
////                return;
////                label215: if (str3.equals("02"))
////                    str4 = BluetoothService.this.getResources().getString(2131165425);
////                else if (str3.equals("03"))
////                    str4 = BluetoothService.this.getResources().getString(2131165430);
////                else if (str3.equals("04"))
////                    str4 = BluetoothService.this.getResources().getString(2131165429);
////                else if (str3.equals("05"))
////                    str4 = BluetoothService.this.getResources().getString(2131165428);
////                else if (str3.equals("06"))
////                    str4 = BluetoothService.this.getResources().getString(2131165424);
////            }
////            label350: float f = (float)(Integer.parseInt(str2 + str3, 16) / 10.0D);
////            Log.e(Tag, "测量温度 ：" + f + "℃");
////            BluetoothService.this.sendBroadCast("ble.datavalue", "tempvalue", f);
//        }
//
//        public void onCharacteristicWrite(BluetoothGatt paramAnonymousBluetoothGatt, BluetoothGattCharacteristic paramAnonymousBluetoothGattCharacteristic, int paramAnonymousInt) {
//        }
//
//        public void onConnectionStateChange(BluetoothGatt paramAnonymousBluetoothGatt, int paramAnonymousInt1, int paramAnonymousInt2) {
////            Log.e(BluetoothService.this.Tag, "status=" + paramAnonymousInt1 + "  newState=" + paramAnonymousInt2);
////            BluetoothService.this.stopConnectTimer();
////            if (paramAnonymousInt1 == 0)
////            {
////                if (paramAnonymousInt2 == 2)
////                {
////                    Log.e(BluetoothService.this.Tag, "连接成功 ");
////                    bool3 = paramAnonymousBluetoothGatt.discoverServices();
////                    Log.e(BluetoothService.this.Tag, "发现服务 ： " + bool3);
////                }
////                while (paramAnonymousInt2 != 0)
////                {
////                    boolean bool3;
////                    return;
////                }
////                Log.e(BluetoothService.this.Tag, "断开连接");
////                boolean bool2 = BluetoothService.this.refreshDeviceCache();
////                Log.e("ser", "==断开连接 ；======清除缓存 ： " + bool2);
////                sendBroadCast("ble.disconnect");
////                BluetoothService.this.disconnect();
////                BluetoothService.this.startTimer();
////                return;
////            }
////            Log.e(BluetoothService.this.Tag, "回调异常");
////            boolean bool1 = BluetoothService.this.refreshDeviceCache();
////            Log.e("ser", "==回调异常 ；======清除缓存 ： " + bool1);
////            BluetoothService.this.sendBroadCast("ble.disconnect");
////            disconnect();
////            startTimer();
//        }
//
//        public void onServicesDiscovered(BluetoothGatt paramAnonymousBluetoothGatt, int paramAnonymousInt) {
//            if (paramAnonymousInt == 0)
//            {
//                BluetoothGattService localBluetoothGattService = paramAnonymousBluetoothGatt.getService(UUID.fromString("00001910-0000-1000-8000-00805f9b34fb"));
//                localBluetoothGattService.getCharacteristic(UUID.fromString("0000fff2-0000-1000-8000-00805f9b34fb"));
//                if ((0x10 & BluetoothService.this.mBluetoothGattCharacteristic_Notify.getProperties()) > 0)
//                {
//                    Log.e("Ble", "具有通知属性。。。");
//                    BluetoothService.this.mBluetoothGatt.setCharacteristicNotification(BluetoothService.this.mBluetoothGattCharacteristic_Notify, true);
//                    BluetoothGattDescriptor localBluetoothGattDescriptor = BluetoothService.this.mBluetoothGattCharacteristic_Notify.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
//                    if (localBluetoothGattDescriptor != null)
//                    {
//                        localBluetoothGattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
//                        Log.e("Ble", BluetoothService.this.mBluetoothGattCharacteristic_Notify.getUuid() + "激活通知属性： " + BluetoothService.this.mBluetoothGatt.writeDescriptor(localBluetoothGattDescriptor));
//                        BluetoothService.this.sendBroadCast("ble.notifyChange");
//                        GbApplication.Chable = true;
//                    }
//                }
//                return;
//            }
//            Log.e(BluetoothService.this.Tag, "--Service discovery failed");
//        }
//    };
//
//    private void sendBroadCast(String paramString) {
//        sendBroadcast(new Intent(paramString));
//    }
//
//    private void sendBroadCast(String paramString1, String paramString2, float paramFloat) {
//        Intent localIntent = new Intent(paramString1);
//        localIntent.putExtra(paramString2, paramFloat);
//        sendBroadcast(localIntent);
//    }
//
//    private void sendBroadCast(String paramString1, String paramString2, String paramString3) {
//        Intent localIntent = new Intent(paramString1);
//        localIntent.putExtra(paramString2, paramString3);
//        sendBroadcast(localIntent);
//    }
//
//    private void stopConnectTimer() {
//        if (this.timerDis != null) {
//            this.timerDis.cancel();
//            this.timerDis = null;
//        }
//        if (this.taskDis != null) {
//            this.taskDis.cancel();
//            this.taskDis = null;
//        }
//    }
//
//    private void timetaskConnect() {
//        Log.e(this.Tag, "----------------进入等待 3 秒 ");
//        stopConnectTimer();
//        taskDis = new TimerTask() {
//            public void run() {
//                Log.e(BluetoothService.this.Tag, "----------------3秒后   状态 ： " + GbApplication.Chable);
//                if (!GbApplication.Chable) {
//                    disconnect();
//                    startTimer();
//                }
//            }
//        };
//        this.timerDis = new Timer();
//        this.timerDis.schedule(this.taskDis, 4000L);
//    }
//
//    public boolean connect(String address) {
//        Log.e("connect address", address);
//        GbApplication.Address = address;
//        if (mBluetoothAdapter == null) {
//            return false;
//        } else {
//            mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(address);
//        }
//        if (mBluetoothDevice == null) return false;
//        mBluetoothGatt = mBluetoothDevice.connectGatt(this, false, mGattcallback);
//        bleAddress = address;
//        return true;
//    }
//
//    public void disconnect() {
//        if ((mBluetoothAdapter == null) || (mBluetoothGatt == null))
//            return;
//        if (mBluetoothGatt != null) {
//            mBluetoothGatt.disconnect();
//            if (mBluetoothGatt != null)
//                mBluetoothGatt.close();
//            mBluetoothGatt = null;
//        }
//        GbApplication.Chable = false;
//    }
//
//    public boolean initialize() {
//        if (mBluetoothManager == null) {
//            mBluetoothManager = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE));
//            if (mBluetoothManager == null)
//                return false;
//        }
//        if (mBluetoothAdapter == null) {
//            mBluetoothAdapter = mBluetoothManager.getAdapter();
//            if (mBluetoothAdapter == null)
//                return false;
//        }
//        GbApplication.mBluetoothAdapter = mBluetoothAdapter;
//        if (!mBluetoothAdapter.isEnabled())
//            mBluetoothAdapter.enable();
//        return true;
//    }
//
//    @Nullable
//    @Override
//    public IBinder onBind(Intent intent) {
//        return ibinder;
//    }
//
//
//    public void openBluetooth() {
//        if (!this.mBluetoothAdapter.isEnabled())
//            this.mBluetoothAdapter.enable();
//    }
//
//    public boolean refreshDeviceCache() {
//        BluetoothGatt localBluetoothGatt1 = this.mBluetoothGatt;
//        boolean bool1 = false;
//        if (localBluetoothGatt1 != null) ;
//        try {
//            BluetoothGatt localBluetoothGatt2 = this.mBluetoothGatt;
//            Method localMethod = localBluetoothGatt2.getClass().getMethod("refresh", new Class[0]);
//            bool1 = false;
//            if (localMethod != null) {
//                boolean bool2 = ((Boolean) localMethod.invoke(localBluetoothGatt2, new Object[0])).booleanValue();
//                bool1 = bool2;
//            }
//            return bool1;
//        } catch (Exception localException) {
//            Log.e("#############", "An exception occured while refreshing device");
//        }
//        return false;
//    }
//
//    public void startTimer() {
//        stopTimer();
//        if (timer == null)
//            timer = new Timer();
//        if (timerTask == null)
//            timerTask = new TimerTask() {
//                public void run() {
//                    listDevice.clear();
//                    mBluetoothAdapter.startLeScan(leScanCallback);
//                    try {
//                        Thread.sleep(3000L);
//                        mBluetoothAdapter.stopLeScan(leScanCallback);
//                        return;
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            };
//        if ((timer != null) && (timerTask != null))
//            timer.schedule(timerTask, 500L, 5000L);
//    }
//
//    public void stopTimer() {
//        if (timer != null) {
//            timer.cancel();
//            timer = null;
//        }
//        if (timerTask != null) {
//            timerTask.cancel();
//            timerTask = null;
//        }
//    }
//
//
//    public class BLEBinder extends Binder {
//        public BLEBinder() {
//        }
//
//        public BluetoothService getService() {
//            return BluetoothService.this;
//        }
//    }
//
//}
